const App = getApp();
Page({
    data: {
        imgUrl:App.globalData.imgUrl,
        isLoading: true,
        reLoading: false,
        key: '',
        detail: {},
        qrcode: '',
        isFullSucreen: App.globalData.isFullSucreen, //判断机型
    },
    onLoad: function (options) {
        let _this = this;
        _this.setData({
            id: options.id
        });
        _this.getDetail();
        // _this.getPoster();
    },

    // 获取赋能详情
    getDetail() {
        let _this = this;
        App._get('Emptyqrcodec/getCodeInfo', {
            id: _this.data.id
        }, res => {
            _this.setData({
                detail:res.data
            })
            // _this.getFeatures(res.data.detail.type);
        }, false, () => {
            _this.setData({
                isLoading: false,
                reLoading: false,
            })
        });
    },

    // 获取首页模块详情
    getFeatures(type) {
        let _this = this;
        App._get('features/getTypeInfo', {type}, res => {
            _this.setData(res.data)
        });
    },

    // 获取海报图片
    getPoster() {
        let _this = this;
        App._get('Generate/poster', {
            key: _this.data.key
        }, res => {
            _this.setData({
                qrcode: res.data.qrcode
            })
        });
    },

    // 复制文字成功
    copyText(e) {
        let _this = this;
        wx.setClipboardData({
            data: e.currentTarget.dataset.text,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {
                        wx.showToast({
                            title: '复制成功'
                        })
                    }
                })
            }
        })
    },

    // 赋能次数统计
    toLog() {
        let _this = this;
        wx.navigateTo({
            url: '/pages/generate/log?generate_id=' + _this.data.detail.generate_id
        })
    },
    // 修改赋能信息
    onEditQrcode(){
      
      let info = {
        ad_img: this.data.detail.f_command_type == 2 ? this.data.detail.ad_img : JSON.parse(this.data.detail.f_option).ad_img,
        f_command_type: this.data.detail.f_command_type,
        id: this.data.detail.id,
        f_type: this.data.detail.f_type,
        fnType: this.data.detail.fnType,
        f_adver_type: this.data.detail.f_adver_type,
        f_area_id: this.data.detail.has_area ? this.data.detail.has_area.id : '',
        f_area_name: this.data.detail.has_area ? this.data.detail.has_area.f_area_name : '',
        f_command_type_w: this.data.detail.f_command_type_w
      }
      // return;
      wx.navigateTo({
        url: '/pages/salesman/batchSetEdit/batchSetEdit?info=' + JSON.stringify(info),
      })
    },
    // 下载二维码
    onSaveQrcode() {
        let _this = this;
        _this.setData({
            reLoading: true
        })
        // 下载图片二维码
        wx.downloadFile({
            url: _this.data.detail.f_url,
            success(res) {
                wx.hideLoading();
                // 图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(data) {
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        });
                    },
                    fail(err) {
                        console.log(err.errMsg);
                        if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                            wx.showToast({
                                title: "请允许访问相册后重试",
                                icon: "none",
                                duration: 1000
                            });
                            setTimeout(() => {
                                wx.openSetting();
                            }, 1000);
                        }
                    },
                    complete(res) {
                        console.log('complete');
                        // wx.hideLoading();
                        _this.setData({
                            reLoading: false
                        })
                    }
                })
            }
        })
    },


});
